package com.authorization.demo.service.impl;

import com.authorization.demo.entity.Menu;
import com.authorization.demo.entity.MenuRole;
import com.authorization.demo.mapper.MenuRoleMapper;
import com.authorization.demo.service.MenuRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @Remarks:
 * @ClassName: MenuRoleServiceImpl
 * @Author: Micora
 * @CreateTime: 2022/7/20
 */
@Service
@Slf4j
public class MenuRoleServiceImpl extends ServiceImpl<MenuRoleMapper, MenuRole> implements MenuRoleService {


    //返回一个菜单名的list
    @Override
    public List<String> GetMenuNameListByRoleName(String roleName) {
        //创建返回的菜单名List
        List<String> menuNameList = new ArrayList<>();
        QueryWrapper<MenuRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ROLE_NAME", roleName);
        List<MenuRole> menuRoleList = this.list(queryWrapper);
        if (menuRoleList.isEmpty()) {
            return menuNameList;
        }

        Iterator<MenuRole> iterator = menuRoleList.iterator();
        while (iterator.hasNext()) {
            MenuRole next = iterator.next();
            menuNameList.add(next.getMenuName());
        }
        return menuNameList;
    }

    @Override
    public boolean deleteByRoleName(String roleName) {
        QueryWrapper<MenuRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ROLE_NAME", roleName);
        this.remove(queryWrapper);
        //TODO:可以对判断进行完善
        return true;
    }
}
